<?php
/**
 * SHOP 控制台首页
 * ============================================================================
 * 网络科技有限公司，并保留所有权利。
*/

define('IN_ZYS', true);
require(dirname(__FILE__) . '/includes/init.php');

if ($_REQUEST['act'] == ''){
    $smarty->display('index.html');
//首页内容
}elseif($_REQUEST['act'] == 'update'){
	
    $sql="select value from ".ly()."shop_config where code = 'shop_logo'";
    $logo=$db->getOne($sql);
    $smarty->assign("logo",$logo);
    $sql="select store_range,value from ".ly()."shop_config where code in ('shop_country','shop_province','shop_city')";
    $res=$db->getAll($sql);
    $address["country"]=$res[1];
    $address["province"]=$res[2];
    $address["city"]=$res[0];
    $smarty->assign("address",$address);
    $sql="select parent_id,region_id,region_name from ".ly()."region";
    $area=$db->getAll($sql);
    $smarty->assign("area",$area);
	
	if($_POST){
	    $shop_country=$_POST["shop_country"];
	    $shop_province=$_POST["shop_province"];
	    $shop_city=$_POST["shop_city"];
	    if($shop_country!=0){
            $sql="select region_name from ".ly()."region where region_id='{$shop_country}'";
            $res=$db->getOne($sql);
            $_POST["shop_country"]=$res;
            $sql="update ".ly()."shop_config set store_range='{$shop_country}' where code='shop_country'";
            mysql_query($sql);
        }else{
            $_POST["shop_country"]='无';
        }
        if($shop_province!=0){
            $sql="select region_name from ".ly()."region where region_id='{$shop_province}'";
            $res=$db->getOne($sql);
            $_POST["shop_province"]=$res;
            $sql="update ".ly()."shop_config set store_range='{$shop_province}' where code='shop_province'";
            mysql_query($sql);
        }else{
            $_POST["shop_province"]='无';
        }
        if($shop_city!=0){
            $sql="select region_name from ".ly()."region where region_id='{$shop_city}'";
            $res=$db->getOne($sql);
            $_POST["shop_city"]=$res;
            $sql="update ".ly()."shop_config set store_range='{$shop_city}' where code='shop_city'";
            mysql_query($sql);
        }else{
            $_POST["shop_city"]='无';
        }
        $ids = implode(',', array_keys($_POST));
        $ids = array_keys($_POST);
        $res='';
        for($i=0;$i<count($ids);$i++){
            if($i==count($ids)-1){
                $res.="'".$ids[$i]."'";
            }else{
                $res.="'".$ids[$i]."',";
            }
        }
        if($_FILES['shop_logo']['error']==0){
            require("../includes/upload_admin.php");//加载图片上传类
            $up=new upphoto;
            $up->get_ph_tmpname($_FILES['shop_logo']['tmp_name']);
            $up->get_ph_type($_FILES['shop_logo']['type']);
            $up->get_ph_size($_FILES['shop_logo']['size']);
            $up->get_ph_name($_FILES['shop_logo']['name']);
            $up->save();
            $value=$up->ph_name;
            $sql="update ".ly()."shop_config set value='{$value}' where code='shop_logo'";
            mysql_query($sql);
        }
        $sql="UPDATE ".ly()."shop_config SET value = CASE code ";
        foreach($_POST as $key => $value){
          $sql.="WHEN '".$key."' THEN '".$value."' ";
        }
        $sql.="end where code in ($res)";
        $res=mysql_query($sql);
        if($res==1){
            $href[]=array('text'=>"返回站点设置",'href'=>'index.php?act=update');
            sys_msg("操作成功！",$href);
        }else{
            $href[]=array('text'=>"重新编辑",'href'=>'history.back()');
            sys_msg("操作失败！",$href,false);
        }

    }
	$smarty->assign("time",time());
	$smarty->assign("name",$_CFG);
	$smarty->display('update.html');
}elseif($_REQUEST['act'] == 'bot'){
	
	$href[] = array('text' => "返回上一页", 'href' => 'index.php?act=update');
	$href[] = array('text' => "重新编辑", 'href' => 'ads.php?act=ads_position');
	
	
	sys_msg("支付方式修改成功！",$href);
	$smarty->display('bot.html');
}




function arrayToObject($e){

    if( gettype($e)!='array' ) return;
    foreach($e as $k=>$v){
        if( gettype($v)=='array' || getType($v)=='object' )
            $e[$k]=(object)arrayToObject($v);
    }
    return (object)$e;
}
?>
